home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / excel / we0801.exe / WE0801.TXT < prev   
Text File  |  1993-06-08  |  23KB  |  508 lines

  1. ======================================================================
  2.   Microsoft(R) Product Support Services Application Note (Text File)
  3.                  WE0801: OPENING AND SAVING TEXT FILES
  4. ======================================================================
  5.                                                    Revision Date: 5/93
  6.                                                                 1 Disk
  7.  
  8. The following information applies to Microsoft(R) Excel for
  9. Windows(TM), version 4.0.
  10.  
  11.   --------------------------------------------------------------------
  12. | INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY     |
  13. | ACCOMPANY THIS DOCUMENT (collectively referred to as an Application |
  14. | Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER      |
  15. | EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED      |
  16. | WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR       |
  17. | PURPOSE. The user assumes the entire risk as to the accuracy and    |
  18. | the use of this Application Note. This Application Note may be      |
  19. | copied and distributed subject to the following conditions:  1) All |
  20. | text must be copied without modification and all pages must be      |
  21. | included;  2) If software is included, all files on the disk(s)     |
  22. | must be copied without modification (the MS-DOS(R)  utility         |
  23. | diskcopy is appropriate for this purpose);  3) All components of    |
  24. | this Application Note must be distributed together;  and  4) This   |
  25. | Application Note may not be distributed for profit.                 |
  26. |                                                                     |
  27. | Copyright (C) 1993 Microsoft Corporation.  All Rights Reserved.     |
  28. | Microsoft and MS-DOS are registered trademarks and Windows is a     |
  29. | trademark of Microsoft Corporation.                                 |
  30.  ---------------------------------------------------------------------
  31.                                    
  32.  
  33. OVERVIEW
  34. ========
  35.  
  36. This Application Note discusses how Microsoft Excel parses text and
  37. formats numbers and alphanumeric characters in text files. In
  38. addition, it includes the Text Reader add-in macro (TXTRDR.XLA), which
  39. you can use to import text files. This macro parses your text
  40. according to a delimiter you select and prevents Microsoft Excel from
  41. applying automatic number formatting to the entries in your file.
  42.  
  43.                                    
  44.                                    
  45. INTRODUCTION
  46. ============
  47.  
  48. In Microsoft Excel version 4.0, you can open and save files in text
  49. file format. Your text file can consist of rows of text in a single
  50. column or rows and columns of text. When you open a text file in
  51. Microsoft Excel, the way the text is broken into columns depends on
  52. the character you choose to delimit your fields or columns of text.
  53. You can also choose no delimiter and parse your text after the file is
  54. opened. The most common column delimiters are commas, tabs, and
  55. spaces.
  56.  
  57. When you open or save text files in Microsoft Excel, you may find that
  58. some of the text entries in your file do not appear as expected. These
  59. unexpected results may occur because of the way Microsoft Excel parses
  60. text and applies formatting to numbers and alphanumeric characters.
  61.  
  62.                                    
  63.                                    
  64. OPENING AND PARSING TEXT FILES
  65. ==============================
  66.  
  67. How Text Is Parsed
  68. ------------------
  69.  
  70. When you open a text file, Microsoft Excel checks the file for the
  71. column delimiter you selected in the Text File Options dialog box
  72. (from the File menu, choose Open and then choose the Text button). In
  73. this dialog box, the default Column Delimiter option is Tab; you can
  74. also select the Comma, Space, Semicolon, None, or Custom option (when
  75. you select Custom, you can specify a custom delimiter). Once you
  76. choose a column delimiter, Microsoft Excel places the text following
  77. each instance of the specified delimiter in a separate cell. For
  78. example, if your text is delimited by spaces and if you select the
  79. Space delimiter, Microsoft Excel places the text following each space
  80. in a separate cell. This process is called parsing and occurs
  81. automatically when you specify a delimiter for your text file.
  82.  
  83. If you do not select the Space delimiter before you open this space-
  84. delimited file, or if you select the None (no delimiter) option, the
  85. text in your file will not be parsed; that is, each line of text will
  86. appear in a separate row in column A only. In this case, after you
  87. open the file in Microsoft Excel, you can break the text into separate
  88. columns using one of the parsing commands.
  89.  
  90. The parsing commands, located on the Data menu, are:
  91.  
  92.  - Parse - This command parses text based on a fixed length or number
  93.    of characters per column, rather than parsing your text based on a
  94.    specified character.
  95.  
  96.    -and-
  97.  
  98.  - Smart Parse - This command is added to the Data menu when you load
  99.    the Flat File add-in macro. Smart Parse gives you the option of
  100.    parsing your data based on a specified delimiter; this is similar
  101.    to specifying a delimiter in the Text File Options dialog box, with
  102.    the exception that the parsing is done after you've opened the text
  103.    file and there is not an option to parse based on a tab delimiter.
  104.  
  105.   NOTE: When you open a file with a .CSV extension, Microsoft Excel
  106.   for Windows will always assume that this file is a comma-separated
  107.   value (CSV) file regardless of the selected column delimiter. For
  108.   files with any other filename extension, for example, .TXT,
  109.   Microsoft Excel will use the delimiter you specify in the Text File
  110.   Options dialog box.
  111.  
  112. How Text Entries Are Formatted
  113. ------------------------------
  114.  
  115. The following example illustrates how Microsoft Excel treats different
  116. types of entries when you open a text file with a specified delimiter
  117. or when you parse a text file after it's been opened. If you have a
  118. text file containing the following data, where each entry is separated
  119. by a tab
  120.  
  121.    09876     00000001  1/93  12-31-92 2E9
  122.    12345     67-890    01234-5678     alpha     text string
  123.  
  124. and you select the Tab delimiter in the Text File Options dialog box,
  125. the entry following each tab is placed in a separate column and each
  126. line is placed on a separate row. Each text string is evaluated when
  127. it is placed in its respective cell. If the contents can be
  128. interpreted as a value, Microsoft Excel will attempt to apply a built-
  129. in number format to that value. If there is no corresponding number
  130. format, the contents will be interpreted as text and placed in the
  131. cell as is.
  132.  
  133. When you open this file in Microsoft Excel, it will resemble the
  134. following example:
  135.  
  136.       |    A   |     B     |      C      |       D   |       E
  137.    ---|------- |-----------|-------------|-----------|---------------
  138.    1  |   9876 |         1 |      Jan-93 |  12/31/92 |        2.00E+09
  139.    ---|--------|-----------|-------------|-----------|----------------
  140.    2  | 12345  | 67-890    | 01234-5678  |  alpha    |    text string
  141.  
  142. Note that the appearance of some text entries changes. Entries that
  143. are interpreted as values are right-aligned and those that are
  144. interpreted as text are left-aligned -- these are the alignment
  145. defaults for Microsoft Excel.
  146.  
  147. The following table explains how each entry from the sample data is
  148.    formatted in Microsoft Excel.
  149.  
  150.    Original     In Microsoft   How entry is formatted in
  151.    entry        Excel          Microsoft Excel
  152.    -------------------------------------------------------------------
  153.    09876 and    9876 and 1     These entries are interpreted as
  154.    00000001                    values in the General number format.
  155.                                Because leading zeros in a number are
  156.                                insignificant, Microsoft Excel drops
  157.                                the zeros.
  158.    
  159.  
  160.    1/93         Jan-93         If there are any hyphens or slashes
  161.                                separating values, Microsoft Excel
  162.                                will examine the individual values to
  163.                                see if they can be construed as a day
  164.                                and/or a month and/or a year. If so, a
  165.                                date format is applied. Because the 1
  166.                                can be interpreted as a month value
  167.                                and 93 is a valid year, this entry
  168.                                matches the mmm-yy built-in number
  169.                                format.
  170.    
  171.    12-31-92     12/31/92       Because the individual values
  172.                                separated by the hyphens can be
  173.                                interpreted as a valid month, day, and
  174.                                year, Microsoft Excel applies the
  175.                                closest matching date format, m/d/yy.
  176.                                Note that when the format is applied,
  177.                                the hyphens are replaced with slashes.
  178.    2E9          2.00E+09       This entry is interpreted to match the
  179.                                0.00E+00 built-in number format.
  180.   
  181.                                  NOTE: If the item had been 2x9,
  182.                                  where x is any letter other than e
  183.                                  or E, the item would have remained a
  184.                                  text string and would not have
  185.                                  changed.
  186.    
  187.    12345        12345          This entry matches the General built-
  188.                                in number format. The application of
  189.                                this format does not alter the value's
  190.                                appearance because there are no
  191.                                leading zeros.
  192.    
  193.    67-890 and   67-890 and     These entries
  194.    01234-5678   01234-5678     are interpreted as text because
  195.                                hyphens separate the numbers and
  196.                                because the numbers on either side of
  197.                                the hyphens cannot be interpreted as a
  198.                                month, day, or year.
  199.    
  200.    alpha and    alpha and      These entries are not changed because
  201.    text string  text string    they are text.
  202.  
  203. Using Custom Number Formats to Change Text Entries
  204. ---------------------------------------------------
  205.  
  206. You can use custom number formats to return entries to their original
  207. appearance (that is, the way they looked before you opened the text
  208. file in Microsoft Excel). For example, to change 9876 to 09876, do the
  209. following:
  210.  
  211. 1. Select cell A1 (the cell where the value is located).
  212.  
  213. 2. From the Format menu, choose Number.
  214.  
  215. 3. In the Code box, type 00000. This formats the number to always have
  216.    5 digits, displaying leading zeros even when you've entered a
  217.    single digit value. For example, with this number format applied, 1
  218.    will be displayed as 00001, and 25 will be displayed as 00025.
  219.  
  220. 4. Choose OK to accept the number format. 9876 is now displayed as
  221.    09876.
  222.  
  223. In date formats in Microsoft Excel, hyphens and slashes are
  224. interchangeable. If you would rather use hyphens than slashes (or vice
  225. versa), apply a custom number format that uses the desired character.
  226. For example, to change the value 12/31/92 to 12-31-92, apply a m-d-yy
  227. custom number format .
  228.  
  229.     Important: If you apply custom number formats or make other
  230.     formatting changes to font size, borders, shading, row height, or
  231.     column width, you must save the file in the Normal file format to
  232.     retain the formatting. (From the File menu, choose Save As and
  233.     under Save File As Type, select the Normal format.)
  234.  
  235. Some entries cannot be returned to their original appearance with a
  236. custom number format. For example, there is no custom number format
  237. that will restore 2.00E+09 to 2E9. To retain the format 2E9, you can
  238. either add a text character to the string using a word processor or
  239. other text editor, or you can use the Text Reader add-in macro to
  240. import the file. For information on how to use the Text Reader add-in
  241. macro, see "Importing Text Files with the Text Reader Add-in Macro" in
  242. this Application Note.
  243.  
  244. For more information about custom number formats, see pages 221-227 of
  245. "User's Guide 1" or see the "Creating or Deleting Custom Number
  246. Formats" topic under "Number Formats" in Microsoft Excel Help.
  247.  
  248.                                    
  249.  
  250. SAVING AND EXPORTING TEXT FILES
  251. ===============================
  252.  
  253. In Microsoft Excel, you can save worksheets as text files in a comma-
  254. delimited (CSV) or tab-delimited (Text) text format. You can select
  255. either one of these options by choosing Save As from the File menu and
  256. selecting the desired format from the Save File As Type list. You can
  257. also export data from your worksheet to a text file that is space
  258. delimited using the Flat File add-in macro discussed in this
  259. Application Note. After you load this add-in macro, the Export command
  260. will be available on the Data menu. For additional information on
  261. using the Flat File add-in macro, see pages 151-152 of the "User's
  262. Guide 2".
  263.  
  264.  
  265. How Text Entries Are Formatted
  266. -------------------------------
  267.  
  268. In Microsoft Excel, when you save a file in CSV or Text file format,
  269. each entry in the file retains its number format.
  270.   
  271.   NOTE: In the following example, the value 2E9 in cell E1 must be
  272.   entered as a text value (by preceding the entry with an apostrophe)
  273.   to prevent it from being converted to the scientific notation
  274.   format.
  275.  
  276. For example, if you have a worksheet that resembles the following
  277.  
  278.       |    A   |       B     |      C      |      D      |      E
  279.    ---|------- |-------------|-------------|-------------|----------
  280.    1  |   9876 |    00000001 |        1/93 |    12/31/92 | 2E9
  281.    ---|--------|-------------|-------------|-------------|----------
  282.    2  | 12345  | 67-890      | 01234-5678  | alpha       | text string
  283.  
  284. when you open the file in a text editor, the table will resemble one
  285. of the following examples depending on how you saved the file in
  286. Microsoft Excel:
  287.  
  288.  - If you saved the file in the CSV file format, the file will
  289.    resemble the following:
  290.  
  291.          09876,00000001,1/93,12-31-92,2E9
  292.          12345,67-890,01234-5678,alpha,text string
  293.  
  294.  - If you saved the file in the Text file format, the file will
  295.    resemble the following:
  296.  
  297.          09876     00000001  1/93  12-31-92      2E9
  298.          12345     67-890    01234-5678  alpha   text string
  299.  
  300. Using the Flat File Add-in Macro to Export Data
  301. -----------------------------------------------
  302.  
  303. When you export a text file, you can use the export feature of the
  304. Flat File add-in macro to retain not only the built-in and custom
  305. number formats of your selection, but the alignment of the cell
  306. contents as well. The resulting text file will contain evenly aligned
  307. columns.
  308.  
  309. When you load the Flat File add-in macro, two commands are added to
  310. the Data menu:
  311.  
  312.  - SMART PARSE - Use this command to parse a text file after it is
  313.    opened.
  314.  
  315.    -and-
  316.  
  317.  - EXPORT - Use this command to export selected data to a space-
  318.    delimited text file.
  319.  
  320. To retain custom number formats and the alignment of the cell
  321. contents, choose Export from the Data menu and select the Retain Cell
  322. Formats option in the Export dialog box. The Retain Cell Formats
  323. option allows you to retain number formats and alignments in the cell
  324. entries.
  325.  
  326. If you export the sample data with the Retain Cell Formats option
  327. enabled, the result will be:
  328.  
  329.    09876  00000001        1/93  12-31-92  2E9
  330.    12345  67-890    01234-5678  alpha     text string
  331.  
  332. If you want evenly aligned output, where number formats are retained
  333. and alignments are the same, do the following before you export your
  334. data:
  335.  
  336. 1. Select the cells you want to export.
  337.  
  338. 2. From the Format menu, choose Alignment.
  339.  
  340. 3. Under Horizontal, select the Left or Right option (instead of the
  341.    General option).
  342.  
  343. If you then export this sample data with the Retain Cell Formats
  344. option enabled, your data will resemble the following (assuming that
  345. you choose the Left option in the previous procedure):
  346.  
  347.    09876  00000001  1/93     12-31-92  2E9
  348.    12345  67-890    01234-5678  alpha     text string
  349.  
  350. If you do not retain cell formats, the numbers will revert to the
  351. General number format; for example, dates will be converted to their
  352. appropriate serial numbers and all entries will be left-aligned --
  353. regardless of whether a cell contains text or numbers.
  354.  
  355. For example, if you export this sample data when the Retain Formats
  356. option is not enabled, your data will resemble the following:
  357.  
  358.    9876   1       33970          33969     2E9
  359.    12345  67-890  01234-5678   alpha  text string
  360.  
  361.   NOTE: When cells are formatted using the Justify, Fill, or Center
  362.   Across Selection alignment options, choosing the Export command will
  363.   result in the error message, "Error writing to disk."
  364.  
  365. For more information about the Flat File add-in macro, see pages 151-
  366. 152 of "User's Guide 2".
  367.                                    
  368.                                    
  369. IMPORTING TEXT FILES WITH THE TEXT READER ADD-IN MACRO
  370. =======================================================
  371.  
  372. You can use the Text Reader (TXTRDR.XLA) add-in macro, provided with
  373. this Application Note, to import a delimited text file into Microsoft
  374. Excel version 4.0 without changing the format or appearance of any
  375. entries in the file. For example, 09876 will still appear as 09876, 12-
  376. 31-92 will still appear as 12-31-92, and so on.
  377.  
  378. When you import a file using the Text Reader add-in macro, the file
  379. itself is not opened. Instead, the data is imported and each entry is
  380. read from the source file and placed in a new worksheet. The Text
  381. Reader add-in macro reads in each text entry individually and adds an
  382. apostrophe to the beginning of the entry. For example, the Text Reader
  383. add-in macro turns 09876 into '09876. Microsoft Excel interprets any
  384. value that is preceded by an apostrophe as a text value. As a result,
  385. when the entry is placed in a cell, it is displayed as it appeared in
  386. the original text file. Once the data has been imported, you can save
  387. the new worksheet in any available file format.
  388.  
  389.   NOTE: This method of importing a text file may take longer than
  390.   opening the file by choosing the Open command from the File menu.
  391.  
  392. To use the Text Reader add-in macro with Microsoft Excel:
  393.  
  394. 1. In File Manager, copy the TXTRDR.XLA file from the enclosed WE0801
  395.    disk to the LIBRARY directory on your hard disk (this directory
  396.    should be located in the same directory where Microsoft Excel is
  397.    installed).
  398.  
  399. 2. Start Microsoft Excel.
  400.  
  401. 3. From the File menu, choose Open. Change to the LIBRARY directory,
  402.    select TXTRDR.XLA from the list of files, and choose OK.
  403.  
  404. To import your text file, do the following:
  405.  
  406. 1. From the File menu, choose Open Text (this command is added to the
  407.    File menu by the Text Reader add-in macro).
  408.  
  409. 2. Select the text file you want to import and choose OK.
  410.  
  411. 3. Specify the appropriate column delimiter. The default delimiter is
  412.    Tab; however, you also have the option to select Comma, Space, or
  413.    Other (if you select Other, you can specify any keyboard character
  414.    to be the delimiter).
  415.  
  416. 4. Choose OK to import the file.
  417.  
  418. Converting Text Entries to Values
  419. ----------------------------------
  420.  
  421. When you use the Text Reader add-in macro, each imported entry is
  422. formatted in text format and placed in a cell. If some of the columns
  423. contain entries that you want to use in calculations, you must convert
  424. these text entries to values. If you attempt to perform calculations
  425. on these cells without first converting them to values, you will
  426. receive a 0 (zero) or a #VALUE! error value.
  427.  
  428. For example, if you import the following text
  429.  
  430.   |       A      |      B     |      C     |     D
  431. --|--------------|------------|------------|---------
  432. 1 | Product No.  | Month-Year | Units Sold | Revenue
  433. --|--------------|------------|------------|---------
  434. 2 | 2e190        | 1-93       | 1000       | 5000
  435. --|--------------|------------|------------|---------
  436. 3 | 2e190        | 2-93       | 2000       | 10000
  437. --|--------------|------------|------------|---------
  438. 4 | 2e190        | 3-93       | 3000       | 15000
  439.  
  440. and want to calculate totals for the entries in the Units Sold and
  441. Revenue columns, do the following to convert these entries to values:
  442.  
  443. 1. In cell E1 (or any blank cell), type  1.
  444.  
  445. 2. With cell E1 selected, choose Copy from the Edit menu.
  446.  
  447. 3. Select cells C2:D4 (the range of cells you want  to convert).
  448.  
  449. 4. From the Edit menu, choose Paste Special. Under Operation, select
  450.    the Multiply option and choose OK.
  451.  
  452. Multiplying each text entry by the number 1 will convert it to a
  453. value. You can then apply number formats and perform calculations.
  454.  
  455. Converting Fractions to Values
  456. ------------------------------
  457.  
  458. The exception to using the method described in the previous section is
  459. when you have fractions that could be interpreted as dates. For
  460. example, if you import the following fractions
  461.  
  462.   |  A
  463. --|------
  464. 1 | 1/4
  465. --|------
  466. 2 | 1/3
  467. --|------
  468. 3 | 1/2
  469. --|------
  470. 4 | 3/4
  471.  
  472. you need to format a blank range of cells with a Fraction number
  473. format and then use a formula to correctly convert them. If you use
  474. the procedure outlined in the previous section (choosing the Paste
  475. Special command and selecting the Multiply option), these fractions
  476. would be converted to dates.
  477.  
  478. To convert these text-formatted fractions to values:
  479.  
  480. 1. Select cells B1:B4 (or a range of blank cells equal in size to the
  481.    range you want to convert to values). This range should remain
  482.    selected through step 7.
  483.  
  484. 2. From the Format menu, choose Number. Under Category, select
  485.    Fraction. Under Format Codes, select  # ?/?. Choose OK.
  486.  
  487. 3. Type the following formula
  488.  
  489.       =VALUE(0&" "&A1)
  490.  
  491.    where A1 is the first cell in the range you want to convert.
  492.  
  493. 4. Hold down the CTRL key and press ENTER. This will automatically
  494.    copy the formula to all cells in the selected range, B1:B4.
  495.  
  496. 5. From the Edit menu, choose Copy.
  497.  
  498. 6. From the Edit menu, choose Paste Special. In the Paste box, select
  499.    the Values option and choose OK. This converts each of the VALUE()
  500.    formulas to their underlying values.
  501.  
  502. 7. From the Edit menu, choose Cut.
  503.  
  504. 8. Select cells A1:A4. From the Edit menu, choose Paste. This replaces
  505.    the text fractions with values.
  506.  
  507. You can now perform calculations using the fractional values.
  508.